<?php

namespace App\Domain\Account\Models;


use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * Class AccountModel
 *
 * @package App\Models
 * @property int $id
 * @property string $uid 唯一标识uid
 * @property string $organization_uid 机构唯一标识uid
 * @property string $organization_name 机构名称
 * @property string $province_code 所在省code
 * @property string $city_code 所在市code
 * @property string $area_code 所在区code
 * @property string $town_code 所在镇code
 * @property string $village_uid 所在村uid
 * @property string $account 用户名（账号）
 * @property string $password 用户密码
 * @property string $salt 加密盐值
 * @property string $name 用户姓名
 * @property string|null $phone 手机号码
 * @property string|null $email 邮箱
 * @property string|null $head 头像
 * @property string|null $sex 性别
 * @property string|null $open_id 微信OPEN ID
 * @property string $last_login_time 最后登录时间
 * @property string|null $last_login_ip 最后登录IP
 * @property string $info 备注
 * @property int $sort 排序
 * @property int $state 是否启用 0：未启用 1：已启用
 * @property int $status 是否删除 0：已删除 1：正常
 * @property string $create_account_uid 创建用户uid
 * @property \Illuminate\Support\Carbon $create_time 创建时间
 * @property string $modify_account_uid 修改用户uid
 * @property \Illuminate\Support\Carbon $modify_time 修改时间
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read int|null $notifications_count
 * @property-read \App\Domain\Account\Models\OrganizationModel $organization
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Domain\Account\Models\PermissionModel[] $permissions
 * @property-read int|null $permissions_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Domain\Account\Models\RoleModel[] $role
 * @property-read int|null $role_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Domain\Account\Models\RoleModel[] $roles
 * @property-read int|null $roles_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel permission($permissions)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel role($roles, $guard = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereAccount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereAreaCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereCityCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereCreateAccountUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereCreateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereHead($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereInfo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereLastLoginIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereLastLoginTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereModifyAccountUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereModifyTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereOpenId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereOrganizationName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereOrganizationUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereProvinceCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereSalt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereTownCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereVillageUid($value)
 * @mixin \Eloquent
 * @property string $sex_dict 性别
 * @property string $sex_dict_code 性别
 * @property-read \App\Domain\Account\Models\DoctorAccountModel $accountDoctor
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Domain\Account\Models\RoleModel[] $roleModel
 * @property-read int|null $role_model_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereSexDict($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereSexDictCode($value)
 * @property-read \App\Domain\Account\Models\DoctorAccountModel $accountOrgDoctor
 * @property string $province_name 所在省名称
 * @property string $city_name 所在市名称
 * @property string $area_name 所在区名称
 * @property string $town_name 所属镇名称
 * @property string|null $village_name 所属村名称
 * @property-read \App\Domain\Account\Models\AddressModel|null $area
 * @property-read \App\Domain\Account\Models\AddressModel|null $city
 * @property-read \App\Domain\Account\Models\AddressModel|null $province
 * @property-read \App\Domain\Account\Models\AddressModel|null $town
 * @property-read \App\Domain\Account\Models\VillageModel|null $village
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereAreaName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereCityName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereProvinceName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereTownName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereVillageName($value)
 * @property string|null $doctor_token pad登录后token
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Domain\Account\Models\AccountModel whereDoctorToken($value)
 */
class AccountModel extends Authenticatable implements JWTSubject
{
    use Notifiable;
    use HasRoles;

    protected $table = 'account';

    const CREATED_AT = 'create_time';

    const UPDATED_AT = 'modify_time';

    /**
     * 守卫名称
     * @var string
     */
    public $guard_name = 'api';

    protected $guarded = [];

    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }


    public function province()
    {
        return $this->hasOne(
            AddressModel::class,
            'code',
            'province_code')->where("code", '<>', '')->where('code', '<>', null);
    }

    public function city()
    {
        return $this->hasOne(
            AddressModel::class,
            'code',
            'city_code')->where("code", '<>', '')->where('code', '<>', null);
    }

    public function area()
    {
        return $this->hasOne(
            AddressModel::class,
            'code',
            'area_code')->where("code", '<>', '')->where('code', '<>', null);
    }

    public function town()
    {
        return $this->hasOne(
            AddressModel::class,
            'code',
            'town_code')->where("code", '<>', '')->where('code', '<>', null);
    }

    public function village()
    {
        return $this->hasOne(
            VillageModel::class,
            'uid',
            'village_uid')->where("uid", '<>', '')->where('uid', '<>', null);
    }

    public function roleModel()
    {
        return $this->hasManyThrough(
            RoleModel::class,
            AccountHasRoles::class,
            'model_id',
            'id',
            'id',
            'role_id'
        );
    }

//    public function accountOrgDoctor()
//    {
//        return $this->hasOne(
//            DoctorAccountModel::class,
//            "account_uid",
//            "uid"
//        );
//    }

    public function accountDoctor()
    {
        return $this->hasOne(
            DoctorAccountModel::class,
            "account_uid",
            "uid"
        );
    }



}
